<?php

/*

	Author:	Cedric McMillan Junior

	Date:	March 15th 2012
	
	Aim:	general purpose utility code

*/




# loads a file [ need to put in an exception here ]
function loadFile($fn="")
{
	if	(!empty($fn))
	{
		if	(file_exists($fn))
		{
			return	file_get_contents($fn);
		}
	}
}




# includes the language array 
function includeLanguageArray($fn)
{
	global $gLanguageString;
	
	# dump it all into memory for now
	require_once("$fn");

	return	$gLanguageString;
}






# we need to get language array 
function getLanguageArray($language)
{
	$gLanguageString	=	array();

	$fn		=	LANGUAGE_ARRAY_PATH;
	$fn		.=	$language	.	".php";

	include("$fn");

	return	$gLanguageString;
}






# resets the file
function resetCommentFile($fileName=LOG_FILE)
{
	if (!$handle = fopen($fileName, 'w+')) { }

	if (fwrite($handle, "") === FALSE) {  } fclose($handle);

}





# logs info to logfile
function addComment($comment = "",$fileName=LOG_FILE)
{
	if	(!file_exists($fileName)) 
	{
		if (!$handle = fopen($fileName, 'w+')) 
		{  
		}
		if (fwrite($handle, "") === FALSE) 
		{  
		}
		fclose($handle);
		chmod("$fileName", 0777);
	}

	# Getting the information
	if	(array_key_exists('REMOTE_ADDR',$_SERVER))
	{
		$ipaddress		=	$_SERVER['REMOTE_ADDR'];
		$page			=	"http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
		$page			.=	iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");
# gives log errors a lot
#		$useragent		=	$_SERVER['HTTP_USER_AGENT'];
		$useragent		=	"";
		# suppress errors
		$remotehost		=	@getHostByAddr($ipaddress);
	}
	else
	{
		$ipaddress		=	"";
		$page			=	"";
		$useragent		=	"";
		$remotehost		=	"";
	}

	$time			=	date("Y-m-d G:i:s");
	
	if (!$handle = fopen($fileName, 'a+')) { echo("Failed to open addcomment log file $fileName"); return;}

	
	$logline	=	$time . '|'  . $page . '|' . $comment . "\n";


	if (fwrite($handle, $logline) === FALSE) { echo("Failed to write to main log file"); } fclose($handle);


}








# helper conditional
function iif($expression, $returntrue, $returnfalse = '') 
{
    return ($expression ? $returntrue : $returnfalse);
}





# this calls the arbitrary function that returns the data we want to cache
function callUserFunction($function_pointer,$params)
{
	$vars	=	explode(",", $params);
	$num	=	count($vars);

	return call_user_func_array($function_pointer,$params);
		
}		
	












